//
//  CAIUpgradeTool.m
//  CHSUpgradeManager
//
//  Created by simon on 2021/9/18.
//

#import "CAIUpgradeTool.h"

@implementation CAIUpgradeTool

+ (BOOL)caiUpgradeIsEmpty:(NSString *)string
{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        return YES;
    }
    return NO;
}


+ (UIWindow *)mainWindow
{
    UIWindow *window = nil;
    if (@available(iOS 13.0,*)) {
        
        NSSet <UIScene *>*connectedScenes = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *scene = nil;
        if (![UIApplication sharedApplication].supportsMultipleScenes) {
            scene = (UIWindowScene *)[[connectedScenes allObjects] firstObject];
        }else{
            for (UIWindowScene * windowScene in connectedScenes) {
                if (windowScene.activationState == UISceneActivationStateForegroundActive) {
                    scene = windowScene;
                }
            }
        }
        id sceneDelegate = scene.delegate;
        if (!sceneDelegate){
            window = [[[UIApplication sharedApplication] delegate] window];
        }
        else
        {
            if ([sceneDelegate respondsToSelector:@selector(setWindow:)])
            {
               window = [sceneDelegate window];
            }
        }
    }else{
        window = [[[UIApplication sharedApplication] delegate] window];
    }
    return window;
}


+ (UIViewController*)currentViewControllerWithCheckPresented:(BOOL)checkCurrentViewPresented;
{
    UIViewController *currentViewController = [[self mainWindow] rootViewController];
    if (checkCurrentViewPresented) {
        while ([currentViewController presentedViewController])
        {
            currentViewController = [currentViewController presentedViewController];
        }
    }
    if([currentViewController isKindOfClass:[UITabBarController class]])
    {
        //返回tabBarController的选中控制器
        if ([(UITabBarController *)currentViewController selectedViewController]) {
            currentViewController = [(UITabBarController *)currentViewController selectedViewController];
        }
        else
        {
            //如果当前没有选中的viewControoller，则返回tabBarController的第一个控制器
            UITabBarController *tabBarController = (UITabBarController *)currentViewController;
            currentViewController = [tabBarController.viewControllers firstObject];
        }
    }
    while ([currentViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)currentViewController topViewController])
        currentViewController = [(UINavigationController*)currentViewController topViewController];
    return currentViewController;
}



+ (NSString *)getJSONSerializationStringFromJSONObject:(nullable id)responseObject
{
    if ([NSJSONSerialization isValidJSONObject:responseObject])
    {
        NSError *error = nil;
    
        if (@available(iOS 13.0, *)) {
            NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingWithoutEscapingSlashes error:&error];
             if (!error && data!=nil)
             {
                 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 return str;
             }
            return nil;
        }else{
            NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingFragmentsAllowed error:&error];
             if (!error && data!=nil)
             {
                 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 return str;
             }
        }
    }
    return nil;
}


+ (UIImage *)bundleImageNamed:(NSString *)name{
    
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"CHSUpgradeManager" ofType:@"bundle"]];
    UIImage *img = [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
    return img;
}
@end
